package com.gitee.zhanyao.codebase.java.file.poi.word;

import com.gitee.zhanyao.codebase.java.file.poi.word.handler.XWPFParagraphHandler;
import com.gitee.zhanyao.codebase.java.file.poi.word.handler.XWPFTableHandler;
import lombok.Data;
import org.apache.poi.xwpf.usermodel.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * 仅支持对docx文件的文本及表格中的内容进行替换
 * 模板仅支持 ${key} 标签
 */
@Data
public class WordTemplate {

    private XWPFDocument document;

    /**
     * 初始化模板内容
     *
     * @param inputStream 模板的读取流(docx文件)
     * @throws IOException
     */
    public WordTemplate(InputStream inputStream) throws IOException {
        document = new XWPFDocument(inputStream);
    }

    public WordTemplate(XWPFDocument xwpfDocument) throws IOException {
        this.document = xwpfDocument;
    }

    public XWPFDocument getXWPFDocument() {
        return document;
    }

    /**
     * 替换模板中的标签为实际的内容
     *
     * @param map
     */
    public void replaceTag(Map<String, String> map) {
        replaceParagraphs(map);
        replaceTables(map);
    }

    /**
     * 替换模板中的标签为实际的内容
     *
     * @param map
     */
    public void replaceTag(Map<String, String> map, List<List<List<String>>> dynamicList, int[] dynamicTableIndexs) {
        replaceParagraphs(map);
        replaceTables(map);
        replaceDynamicTables(dynamicList, dynamicTableIndexs);
    }


    /**
     * 将处理后的内容写入到输出流中
     *
     * @param outputStream 输出流
     * @throws IOException
     */
    public void write(OutputStream outputStream) throws IOException {
        document.write(outputStream);
    }

    /**
     * 替换文本中的标签
     *
     * @param map key(待替换标签)-value(文本内容)
     */
    private void replaceParagraphs(Map<String, String> map) {
        List<XWPFParagraph> allXWPFParagraphs = document.getParagraphs();
        for (XWPFParagraph XwpfParagrapg : allXWPFParagraphs) {
            XWPFParagraphHandler XwpfParagrapgUtils = new XWPFParagraphHandler(XwpfParagrapg);
            XwpfParagrapgUtils.replaceAll(map);
        }
    }

    /**
     * 替换表格中的标签
     *
     * @param map key(待替换标签)-value(文本内容)
     */
    private void replaceTables(Map<String, String> map) {
        List<XWPFTable> xwpfTables = document.getTables();
        for (XWPFTable xwpfTable : xwpfTables) {
            XWPFTableHandler xwpfTableUtils = new XWPFTableHandler(xwpfTable);
            xwpfTableUtils.replace(map);
        }
    }

    /**
     * 替换动态表格
     *
     * @param dynamicList
     * @param dynamicTableIndexs
     */
    private void replaceDynamicTables(List<List<List<String>>> dynamicList, int[] dynamicTableIndexs) {
        List<XWPFTable> xwpfTables = document.getTables();
        int index = 0;
        for (int i = 0; i < xwpfTables.size(); i++) {
            if (dynamicTableIndexs[index] == i) {
                List<List<String>> tableList = dynamicList.get(index);
                XWPFTable table = xwpfTables.get(i);
                XWPFTableRow row = null;
                List<String> rowList = null;
                for (int j = 0; j < tableList.size(); j++) {
                    row = table.createRow();
                    rowList = tableList.get(j);
                    List<XWPFTableCell> cells = row.getTableCells();
                    for (int k = 0; k < rowList.size(); k++) {
                        cells.get(k).setText(rowList.get(k));
                    }
                }
                index++;
            }
        }
    }
}
